// __PUBLIC__/js/page/admin/user/allocation
require(['../../common'], function() {
  require(['user.allocation'])
})

define('user.allocation', [
  'jquery',
  'css!ztree/ztree',
  'ztree/ztree'
], function($) {
  'use strict';

  function onCheck() {
    var zTree = $.fn.zTree.getZTreeObj("tree")
    var nodes = zTree.getCheckedNodes(true)

    var arr = new Array()
    if (nodes.length > 0) {
      for (var i = 0, l = nodes.length; i < l; i++) {
        arr[i] = nodes[i].id
      }

      var rid = $("#hid").val()
      if (confirm(lang.transfer)) {
        $.post(APP + '?app=admin&m=User&a=rightsAllocation', {
          nodeId: arr,
          roleId: rid
        }, function(mes) {
          if (mes == "1") {
            alert(lang.allocationSuccess)
            location.href = APP + '?app=admin&m=User&a=userGroupManage'
          } else {
            alert(lang.allocationFails)
          }
        })
      }
    }
  }

  $(document).ready(function() {
    var setting = {
      check: {
        enable: true
      },
      data: {
        simpleData: {
          enable: true
        }
      }
    }
    $.ajax({
      async: false,
      cache: false,
      type: 'POST',
      dataType: "json",
      url: APP + '?app=admin&m=User&a=getJsonNode', //请求的action路径  
      data: {
        id: $("#hid").val()
      },
      success: function(data) { //请求成功后处理函数。  
        $.fn.zTree.init($("#tree"), setting, data)
      }
    })
  }).on('click', '#J_btn', function(e) {
    e.preventDefault()
    onCheck()
  })

})
